package com.example.e_comm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.e_comm.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT u.* FROM users u " +
            "INNER JOIN user_role ur ON u.id = ur.user_id " +
            "WHERE u.deleted = 0 AND ur.role_id = #{roleId}")
    List<User> getUsersByRoleId(@Param("roleId") Long roleId);

    @Select("SELECT DISTINCT u.* FROM users u " +
            "INNER JOIN user_role ur ON u.id = ur.user_id " +
            "INNER JOIN role_permission rp ON ur.role_id = rp.role_id " +
            "WHERE u.deleted = 0 AND rp.perm_id = #{permId}")
    List<User> getUsersByPermId(@Param("permId") Long permId);
}